import fiveagon.io.*;

List<PImage> imageList;
int gridSize;
int imageSize;

void setup()
{
    size(500, 500);
    smooth();
    noLoop();

    // Loader has to be initialized in setup() when using this constructor
    // or else it won't be able to find the sketch folder
    // and it will throw an IllegalArgumentException.
    // Besides, you probably won't need a Loader anywhere else.
    Loader<PImage> imageLoader = new PImageLoader(this);

    // Load all images. Simple.
    imageList = imageLoader.load();

    // Or only the ones that match a search.
    //imageList = imageLoader.load("2315");

    // Print useful information
    println("Load-able files: ");
    println(imageLoader);
    println("Number of files Loaded: " + imageList.size());

    gridSize = ceil(sqrt(imageList.size()));
    imageSize = min(width, height) / gridSize;

    for (PImage img : imageList) {
        img.resize(imageSize, imageSize);
    }
}

void draw()
{
    background(0);
    for (int i = 0, n = gridSize * gridSize; i < n; i++) {
        int x = i % gridSize * imageSize;
        int y = i / gridSize * imageSize;
        int index = i % imageList.size();
        image(imageList.get(index), x, y);
    }
}

